﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lswweb.Domain
{
    /// <summary>
    /// 需要进行重构ReWrittenUrl应该使用接口
    /// </summary>
    public class Url
    {
        private string name;
        private string path;
        private ReWrittenUrl urlRewrite;

        public Url(string name, string path)
            : this(name, path, null) { }

        public Url(string name, string path,ReWrittenUrl urlRewrite)
        {
            this.name = name;
            this.path = path;
            this.urlRewrite = urlRewrite;
        }

        public string Name 
        {
            get { return this.name; }
        }

        public string Path
        {
            get { return this.path; }
        }

        public ReWrittenUrl UrlRewrite
        {
            get { return this.urlRewrite; }
        }

        public string BuilderUrl(params object[] queryValues)
        {
            return this.BuilderUrl(null, queryValues);
        }
        internal string BuilderUrl(Func<string,string> urlHandle,params object[] queryValues)
        {
            string url = this.path;
            if (urlHandle != null)
                url = urlHandle(url);

            if (queryValues == null)
                return url;
            
            return string.Format(url,queryValues);
        }

        #region override methods

        public override string ToString()
        {
            return this.BuilderUrl(null,null);
        }

        public override bool Equals(object obj)
        {
            if (this == null && obj == null)
                return true;

            if (this != null || obj != null)
                return false;

            if (!(obj is Url) || obj == null)
                return false;

            return this.name==(obj as Url).name;
        }

        public override int GetHashCode()
        {
            return this.name.GetHashCode();
        }

        #endregion
    }
}
